#include "laurin-80211-networks-model.h"
#include "../core/laurin-utils.h"


Laurin80211NetworksModel::Laurin80211ModelRecord::Laurin80211ModelRecord()
{
	add(ssid);
	add(mac);
	add(distance);
	add(type);
}


Laurin80211NetworksModel::Laurin80211NetworksModel(){}


Glib::RefPtr<Gtk::TreeStore> Laurin80211NetworksModel::getStore()
{
	return _store;
}


void Laurin80211NetworksModel::fillStore(LaurinNetworkSet* set)
{
	if(!(_store))
		_store = Gtk::TreeStore::create(_columns);

	_store->clear();

	const std::list<Laurin80211Network> networks = set->getNetworks();

	std::list<Laurin80211Network>::const_iterator b = networks.begin();
	std::list<Laurin80211Network>::const_iterator e = networks.end();

	Gtk::TreeModel::Row r;
	Gtk::TreeModel::Row ap_r;
	Gtk::TreeModel::Row client_r;


	while(b != e)
	{

		// add network record
		r = *(_store->prepend());
		r[_columns.ssid] = b->getNetworkSSID();
		r[_columns.type] = (int) NETWORK;


		// add access points record
		ap_r = *(_store->prepend(r.children()));
		ap_r[_columns.ssid] = "Access Points";
		ap_r[_columns.type] = (int) INFO;


		const std::list<LaurinAccessPoint> accessPoint = b->getAccessPoint();
		std::list<LaurinAccessPoint>::const_iterator ab = accessPoint.begin();
		std::list<LaurinAccessPoint>::const_iterator ae = accessPoint.end();

		while(ab != ae)
		{

			Gtk::TreeModel::Row tmp_r = *(_store->prepend(ap_r.children()));

			tmp_r[_columns.mac] = Glib::ustring(getReadableMAC(ab->getBSSID()));
			tmp_r[_columns.type] = (int) ACCESS_POINT;

			ab++;
		}


		// add clients record
		client_r = *(_store->append(r.children()));
		client_r[_columns.ssid] = "Clients";
		client_r[_columns.type] = (int) INFO;

		const std::list<Laurin80211Station> clients = b->getStations();
		std::list<Laurin80211Station>::const_iterator cb = clients.begin();
		std::list<Laurin80211Station>::const_iterator ce = clients.end();


		while(cb != ce)
		{
			Gtk::TreeModel::Row tmp_r = *(_store->prepend(client_r.children()));

			tmp_r[_columns.mac] = Glib::ustring(getReadableMAC(ab->getMAC()));
			tmp_r[_columns.type] = (int) CLIENT;

			cb++;
		}

		b++;
	}
}
